<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['groups']=pdo_getall('jy_robshop_goods_groups',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
if($_RQ['op']=='list'){
	$this->model('model')->up_cang();
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$tip='每个抢购都是独立设置运营机制！独立的兑换规则！';
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 15;
	}
	$total='';
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'limit'=>array($pindex,$psize),
	);
	$list=pdo_getslice('jy_robshop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_robshop/detail//id='.$item['id'],true);
			if(!$item['goodstype']){
				$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
				$item['goodsprice']=$goods['price'];
				$item['title']=$goods['title'];
				$item['image']=$goods['image'];
			}else{
				$item['title']=$this->btitle.''.$item['creditnum'].'个';
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'抢购列表'),
			array('op'=>'edit','title'=>'创建抢购'),
		),
		'list'=>array('data'=>'list','tip'=>$tip,'del_one'=>true,'list'=>array(
			array('type'=>'string','title'=>'ID','name'=>'id'),
			//array('type'=>'radio','title'=>'抢购类型','name'=>'goodstype','options'=>'goodstype'),
			array('name'=>'image','title'=>'抢购图片','type'=>'image'),
			array('name'=>'title','title'=>'抢购内容','type'=>'string'),
			array('type'=>'select','title'=>'分类','name'=>'groupid','options'=>'groups'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'price','title'=>'抢购价格','type'=>'string'),
			array('name'=>'jian_teamnum','title'=>'每团人数','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'goods_status'),
			
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_robshop/list/setstatus0/','title'=>'一键待启用'),
			array('type'=>'post','url'=>'jy_robshop/list/setstatus1/','title'=>'一键抢购中'),
			array('type'=>'post','url'=>'jy_robshop/list/setstatus2/','title'=>'一键结束'),
			array('type'=>'post','url'=>'jy_robshop/list/setstatus3/','title'=>'一键更下架'),
		)),
	);
	$return['goods_status']=$this->goods_status;
	$return['goodstype']=$this->goodstype;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['goodstype'] && !$post['goodsid']){
			exi('请选择商品！','error');
		}
		if(!$post['title']){
			exi('请输入抢购标题！','error');
		}
		if($post['price']<=0){
			exi('请设置商品抢购价格！','error');
		}
		if($post['goodstype'] && !$post['creditnum']){
			exi('请设置抢购'.$this->btitle.'数量！','error');
		}
		if(!$post['jian_teamnum']){
			exi('请设置设置每个团订单抢购人数！','error');
		}
		if(!$post['zhongnum']){
			exi('请设置设置每个团订单抢中人数！','error');
		}
		if($post['zhongnum']>$post['jian_teamnum']){
			exi('抢中人数不能大于团总人数！','error');
		}
		if(!$post['groupid']){
			exi('请选择分类！','error');
		}
		$data=array(
			'goodstype'=>$post['goodstype'],
			'creditnum'=>$post['creditnum'],
			'goodsid'=>$post['goodsid'],
			'price'=>$post['price'],
			'title'=>$post['title'],
			'desc'=>$post['desc'],
			'jian_openday'=>$post['jian_openday'],
			'jian_open0'=>$post['jian_open0'],
			'jian_open1'=>$post['jian_open1'],
			'jian_open2'=>$post['jian_open2'],
			'jian_hour0'=>ijson_encode($post['jian_hour0']),
			'jian_hour1'=>ijson_encode($post['jian_hour1']),
			'jian_hour2'=>ijson_encode($post['jian_hour2']),
			'jian_days'=>$post['jian_days'],
			'jian_teamnum'=>$post['jian_teamnum'],
			'zhong_jf'=>$post['zhong_jf'],
			'jian_redmax'=>$post['jian_redmax'],
			'jian_redmin'=>$post['jian_redmin'],
			'status'=>$post['status'],
			'xieyi'=>$post['xieyi'],
			'content'=>$post['content'],
			'zhongnum'=>$post['zhongnum'],
			'image'=>$post['image'],
			'groupid'=>$post['groupid'],
			'vipprice'=>$post['vipprice'],
			'is_vipbuy'=>$post['is_vipbuy'],
		);
		if($post['id']){
			$this->update('goods',$data,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=time();
			$this->insert('goods',$data);
		}
		exi('操作成功！','','list');
	}
	if($_RQ['id']){
		$item=$this->get('goods',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['jian_hour0']=$item['jian_hour0']?json_decode($item['jian_hour0'],true):array('start'=>'8:00','end'=>'12:00');
	$item['jian_hour1']=$item['jian_hour1']?json_decode($item['jian_hour1'],true):array('start'=>'13:00','end'=>'15:00');
	$item['jian_hour2']=$item['jian_hour2']?json_decode($item['jian_hour2'],true):array('start'=>'19:00','end'=>'22:00');
	$return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
		'type'=>'edit',
		'title'=>'创建抢购商品',
		'edit_tab'=>true,
		'edits'=>array(
				 array(
				'title'=>'基本设置',
				'list'=>array(
					//array('type'=>'radio','title'=>'抢购类型','name'=>'goodstype','options'=>'goodstype'),
					array('name'=>'goodsid','title'=>'选择商品','type'=>'choose_title','action'=>'jy_weishop/goods.get_goods//noweike=1','help'=>'根据商品可以选择：实物商品，虚拟卡密，虚拟币商品等！'),
					array('type'=>'unitstring','title'=>'抢购'.$this->btitle.'数量','name'=>'creditnum','unit'=>'个','show'=>array('goodstype'=>1)),
					array('type'=>'unitstring','title'=>'抢购价格','name'=>'price','unit'=>'元'),
					array('type'=>'unitstring','title'=>'会员价格','name'=>'vipprice','unit'=>'元'),
					array('type'=>'switch','title'=>'是否开启会员购买权限?','name'=>'is_vipbuy','isradio'=>1),
					array('type'=>'string','title'=>'抢购标题','name'=>'title'),
					array('name'=>'image','title'=>'抢购图片','type'=>'image','show'=>array('goodstype'=>1)),
					array('type'=>'text','title'=>'抢购描述','name'=>'desc'),
					array('type'=>'select','title'=>'分类','name'=>'groupid','options'=>'groups'),
					array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'goods_status'),
					array('type'=>'switch','title'=>'是否开启整天抢购？','name'=>'jian_openday','isradio'=>1),
					array('type'=>'switch','title'=>'是否开启上午抢购时间','name'=>'jian_open0','isradio'=>1,'show'=>array('jian_openday'=>0)),
					array('type'=>'time_hournew','title'=>'上午抢购时间段','name'=>'jian_hour0','show'=>array('jian_openday'=>0)),
					array('type'=>'switch','title'=>'是否开启下午抢购时间','name'=>'jian_open1','isradio'=>1,'show'=>array('jian_openday'=>0)),
					array('type'=>'time_hournew','title'=>'下午抢购时间段','name'=>'jian_hour1','show'=>array('jian_openday'=>0)),
					array('type'=>'switch','title'=>'是否开启晚上抢购时间','name'=>'jian_open2','isradio'=>1,'show'=>array('jian_openday'=>0)),
					array('type'=>'time_hournew','title'=>'晚上抢购时间段','name'=>'jian_hour2','show'=>array('jian_openday'=>0)),
					array('type'=>'unitstring','unit'=>'天','title'=>'设置抢购（N）天','name'=>'jian_days','help'=>'抢购总天数，结束后，商品下架'),
					array('type'=>'unitstring','unit'=>'人','title'=>'设置每个团订单抢购人数','name'=>'jian_teamnum','help'=>'举例设置团订单抢购人数为6人，就是每6人成一团，每团中只有N人抢中得商品+抽奖'),
					array('type'=>'unitstring','unit'=>'人','title'=>'设置每个团订单抢中人数','name'=>'zhongnum','help'=>'举例设置团订单抢购人数为6人，就是每6人成一团，每团中只有N人抢中得商品+抽奖'),
					array('type'=>'unitstring','unit'=>'个','title'=>'设置抢中送（N）积分','name'=>'zhong_jf','help'=>'抢中的送（N）积分'),
					//array('type'=>'unitstring','unit'=>'%','title'=>'失败得红包总波比（%）','name'=>'jian_redmax','help'=>'没抢中的获得红包，按商品百分比金额再随机红包分给【未中的5个人】'),
					//array('type'=>'richtext','title'=>'抢购协议','name'=>'xieyi'),
					array('type'=>'richtext','title'=>'抢购详情','name'=>'content'),
				)
			),
		)
	);
	$return['goods_status']=$this->goods_status;
	$return['goodstype']=$this->goodstype;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择活动','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_robshop_goods',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择活动','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_robshop_goods',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus2'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择活动','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_robshop_goods',array('status'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus3'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择活动','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_robshop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_robshop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_robshop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

?>